#ifndef __VTOR_TIMER_H__
#define __VTOR_TIMER_H__

#ifdef __cplusplus
extern "C" {
#endif

#include "vtor_elec_module_config.h"

#ifdef __VTOR_TIMER__

#include "time.h"

#define VTOR_TIMER_STATE_STOP			0
#define VTOR_TIMER_STATE_RUNNING		1
#define VTOR_TIMER_STATE_READY			2
#define VTOR_TIMER_STATE_WAITING		3


#define VtorTimer_SkipLoop(skipLoopCnt, onSkip)	\
{											\
	static unsigned int curLoopCnt = 0; 	\
	if (curLoopCnt++ < skipLoopCnt) 		\
	{										\
		onSkip;								\
	}										\
	else									\
	{										\
		curLoopCnt = 0;						\
	}										\
}


// 时基单元中断回调，用于精确执行的任务
//#define __VTOR_TIMER_CONFIG_CALLBACK__

// 定时器使用率计算，用于估算任务完成度
//#define __VTOR_TIMER_CONFIG_USAGE__


typedef void (*VtorTimerFun)(void);

typedef struct
{
	VtorTimerFun timerFun; // 将要执行的函数
	int32_t interval;	// 当计次达到interval，执行fun函数
	uint8_t state;		// 状态
	int32_t curTime;	// 当前计次
	int32_t runCnt;		// 本函数运行次数
	int32_t runTime;	// 完成本次任务耗时多久
}VtorTimer;



void VtorTimer_VoidFun(void);

// 实际任务运行
void VtorTimer_Schedule(void);
// 时间片运行
void VtorTimer_IncTick(uint32_t tick);
void VtorTimer_Init(void);

// 获取当前tick，分别是tick数与循环次数
uint32_t VtorTimer_GetTick(void);
uint32_t VtorTimer_GetCnt(void);
void VtorTimer_Delay(uint32_t delayTick);

// 设置与获取当前任务头，由用户调用
void VtorTimer_SetArrayHeader(VtorTimer* header);
VtorTimer* VtorTimer_GetArrayHeader(void);

// 统计定时器占用率
#ifdef __VTOR_TIMER_CONFIG_USAGE__
void VtorTimer_TimerUsageIncTask(void);
void VtorTimer_TimerUsageCntTask(void);
int32_t VtorTimer_GetTimerUsage(void);
#endif

VtorTimer* VtorTimer_Create(VtorTimerFun fun, int32_t interval);
VtorTimer* VtorTimer_Start(VtorTimerFun fun, int32_t interval);
VtorTimer* VtorTimer_Suspend(VtorTimerFun fun, int32_t tick);
void VtorTimer_Stop(VtorTimerFun fun);
void VtorTimer_Delete(VtorTimerFun fun);

void VtorTimer_RunNow(VtorTimerFun fun);
VtorTimer* VtorTimer_GetRunningTimer(void);
VtorTimer* VtorTimer_GetTimerByFun(VtorTimerFun fun);

#ifdef __VTOR_TIMER_CONFIG_CALLBACK__
void VtorTimer_IncTickCallback(uint32_t tick);
#endif
void VtorTimer_DeleteCallback(VtorTimerFun fun);
void VtorTimer_CreateCallback(VtorTimerFun fun);
void VtorTimer_CreateFailCallback(VtorTimerFun fun);

//  time相关
extern time_t VtorTime_timeStamp;
extern struct tm VtorTime_dateTime;
int VtorTime_SetDateTime(int y, int m, int d, int hour, int min, int sec);
int VtorTime_SetTimeStamp(time_t stamp);

#endif // __VTOR_TIMER__

#ifdef __cplusplus
}
#endif

#endif
